package com.example.demo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.example.h5adnative.MainWebActivity;
import com.example.h5adnative.R;

import java.lang.reflect.Method;

import cj.mobile.CJMobileAd;
import cj.mobile.CJSplash;
import cj.mobile.listener.CJInitListener;
import cj.mobile.listener.CJSplashListener;
import cn.xll.nativechannel.MainActivity;
import cn.xll.nativechannel.Utils;

public class WelComeActivity extends AppCompatActivity {

    CJSplash splash = new CJSplash();
    private FrameLayout flSplash;
    private ImageView launcherImageView;

    private boolean isShowAd, isLoadAd, isSetupSuccess;
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        flSplash = findViewById(R.id.splash);
        Utils.getOaid(this);
        Application application = new AppGlobalUtils().getApplication();
        CJMobileAd.init(application, "345a20d3863f8911", new CJInitListener() {
            @Override
            public void initFailed(String errorMsg) {
                Log.e("cj-log", "初始化成功");
            }

            @Override
            public void initSuccess() {
                isSetupSuccess = true;
//                CJRewardVideo.getInstance().setMainActivity(WelComeActivity.this).setIsPreLoad(true).loadAd("ef2e8988cb6485a5");
            }
        });
    }

    private void loadSplash(int width, int height) {
        Log.e("cj-log", "开始调用开屏");
        splash.loadAd(this, "f531cf0880e84a92", width, height, new CJSplashListener() {
            @Override
            public void onShow() {
            }

            @Override
            public void onError(String code, String e) {
                startActivity(new Intent(WelComeActivity.this, MainActivity.class));
                finish();
            }

            @Override
            public void onLoad() {

                isLoadAd = true;
                if (isShowAd) {
                    //展示广告
                    splash.showAd(WelComeActivity.this, flSplash);
                }

            }

            @Override
            public void onClick() {
            }

            @Override
            public void onClose() {
                startActivity(new Intent(WelComeActivity.this, MainWebActivity.class));
                finish();
            }

        });
    }




    @Override
    protected void onResume() {
        super.onResume();
        handler.sendEmptyMessageDelayed(3, 3000);
        handler.sendEmptyMessageDelayed(1, 50);
    }

    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                {
                    if (isSetupSuccess) {
                        loadSplash(flSplash.getWidth(), flSplash.getHeight());
                    } else {
                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (isSetupSuccess) {
                                    loadSplash(flSplash.getWidth(), flSplash.getHeight());
                                } else {
                                    startActivity(new Intent(WelComeActivity.this, MainActivity.class));
                                    finish();
                                }
                            }
                        }, 400);
                    }
                }
                    break;
                case 2:
                    startActivity(new Intent(WelComeActivity.this, MainActivity.class));
                    finish();
                    break;
                case 3:
                    if (isLoadAd) {
                        splash.showAd(WelComeActivity.this, flSplash);
                    }
                    isShowAd = true;
                    break;
            }
//            launcherImageView.setVisibility(View.GONE);
//            startActivity(new Intent(WelComeActivity.this, MainActivity.class));

        }
    };

    public class AppGlobalUtils {
        private Application myApp;

        public Application getApplication() {
            if (myApp == null) {
                try {
                    Method currentApplication = Class.forName("android.app.ActivityThread").
                            getDeclaredMethod("currentApplication");
                    myApp = (Application) currentApplication.invoke(null);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return myApp;
        }
    }
}